package com.example.compete.mapper;

import com.example.compete.DTO.CompanyDTO;
import com.example.compete.DTO.DocumentTypeDTO;
import com.example.compete.DTO.ExecutiveDTO;
import com.example.compete.DTO.CompanyAndExecutiveDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CompanyMapper {

	//查找所有公司信息
    @Select("select * from company_information")
    List<CompanyDTO> findAllCompany();
    
    //查看所有证件类型信息
    @Select("select * from document_type_details")
    List<DocumentTypeDTO> findAllDocumentType();

    //根据公司编号查看公司信息
    @Select("select * from company_information where COMPANY_ID =#{companyId}")
    CompanyDTO findByCompanyId(String companyId);

    //根据公司编号修改公司信息
    @Update("update COMPANY_INFORMATION set COMPANY_NAME = #{companyName}, DOCUMENT_TYPE = #{documentType}, DOCUMENT_NUMBER = #{documentNumber}," +
            "COMPANY_CREATE_TIME = #{companyCreateTime}, REGISTERED_CAPITAL = #{registeredCapital}, ADDRESS = #{address}, EMPLOYEES_NUMBER = #{employeesNumber}," +
            "REGISTERED_TIME = #{registeredTime}, UPDATE_TIME = #{updateTime} where COMPANY_ID=#{companyId}")
    void updateByCompanyId(CompanyDTO companyDTO);

    //添加公司信息
    @Insert("insert into COMPANY_INFORMATION(COMPANY_ID,COMPANY_NAME,DOCUMENT_TYPE,DOCUMENT_NUMBER,COMPANY_CREATE_TIME," +
            "REGISTERED_CAPITAL,ADDRESS,EMPLOYEES_NUMBER,REGISTERED_TIME,UPDATE_TIME) values(#{companyId},#{companyName}," +
            "#{documentType},#{documentNumber},#{companyCreateTime},#{registeredCapital},#{address},#{employeesNumber},#{registeredTime},#{updateTime})")
    void add(CompanyDTO companyDTO);
    
    //关联删除公司和高管信息
    @Delete("delete c1.*, c2.* from company_information c1, company_executive_information c2 where c1.COMPANY_ID = c2.COMPANY_ID and c1.COMPANY_ID = #{companyId}")
    int deleteCompanyAndExecutive(String companyId);
    
    //查找证件类型名称
    @Select("select DOCUMENT_TYPE_NAME from document_type_details where DOCUMENT_TYPE_ID=#{documentTypeId}")
    String selectTypeName(Integer documentTypeId);
    
    //查找公司和高管信息
    @Select("select a.*,b.* from company_information a,company_executive_information b where a.COMPANY_ID = b.COMPANY_ID and COMPANY_ID = #{companyId}")
    CompanyAndExecutiveDTO selectCompanyAndExecutive(String companyId);
    
    //根据公司编号查找公司信息
    @Select("select * from company_information where COMPANY_ID = #{companyId}")
    CompanyDTO selectOneCompanyById(String companyId);
    
    //根据公司编号查找高管信息
    @Select("select * from company_executive_information where COMPANY_ID = #{companyId}")
    List<ExecutiveDTO> findExecutive(String companyId);
    
    //根据公司编号删除公司信息
    @Delete("delete from company_information where COMPANY_ID = #{companyId}")
    void delectOneCompany(String companyId);
    
    //查找是否重复的公司信息
    @Select("select * from company_information where COMPANY_NAME=#{companyName} and DOCUMENT_TYPE=#{documentType} and DOCUMENT_NUMBER=#{documentNumber}")
    List<CompanyDTO> checkCompany(CompanyDTO companyDTO);
}
